﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using CommServer.Common;
using System.Collections.Concurrent;

namespace CommServer.Network {
    public class TcpSession : SocketAsyncEventArgs {
        private object m_SynObject = new object();

        public bool Closed = false;

        public string SessionId;
        public DateTime AcceptTime;
        public DateTime LastRecvTime;
        public DateTime LastRecvProtocolFrameTime;

        public List<byte> DataBuffer = new List<byte>();

        //附加信息
        public bool TagBool;
        public int TagInt;
        public string TagString;
        public object TagObject;

        public void Send(byte[] Data) {
            lock (m_SynObject) {
                if (Data == null) return;
                try {
                    AcceptSocket.Send(Data);
                    Log.Info(TagString, "send:" + Util.BytesToHex(Data));
                } catch (Exception ex) {
                    Log.Error("socket send data error:" + ex.Message+"|"+ex.StackTrace);
                }
            }
        }
        public void Close(ConcurrentQueue<TcpSession> SessionPool)
        {
            lock (m_SynObject)
            {
                if (Closed)
                {
                    return;
                }
                Closed = true;
                //释放非托管资源
                try
                {
                    AcceptSocket.Shutdown(SocketShutdown.Both);
                }
                catch (Exception ex)
                {
                    Log.Error("session close error:" + ex.Message);
                }
                try
                {
                    AcceptSocket.Close();
                    AcceptSocket = null;
                    Log.Info("TCP Session: " + SessionId + "closed ");
                    SessionId = "";
                    DataBuffer.Clear();
                    TagBool = false;
                    TagInt = 0;
                    TagString = "";
                    TagObject = null;

                    if (SessionPool != null)
                        SessionPool.Enqueue(this);
                }
                catch(Exception eee)
                {
                    Log.Error("close session error " + eee.Message + "|" + eee.StackTrace);
                }
            }
        }
       /* public void Close() {
            lock (m_SynObject) {
                if (Closed) {
                    return;
                }
                Closed = true;
                //释放非托管资源
                try {
                    AcceptSocket.Shutdown(SocketShutdown.Both);
                } catch (Exception ex) {
                    Log.Error("session close error:" + ex.Message);
                }
                AcceptSocket.Close();
                AcceptSocket = null;

                SessionId = "";
                DataBuffer.Clear();
                TagBool = false;
                TagInt = 0;
                TagString = "";
                TagObject = null;
            }
        }*/
    }
}
